﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Microsoft.Xna.Framework;

namespace Ecosystème
{
    public class tileMap
    {
        private Texture2D texture;
        private string chemin;
        private int Width;
        private int Height;
        private int[,] textureCont;

        public tileMap(Texture2D uneTexture, string unChemin)
        {
            this.chemin = unChemin;
            this.texture = uneTexture;
        }

        public void chargerMap(string unNom)
        {
            string mapChemin = this.chemin + @"\" + unNom + ".map";
            using (StreamReader sreader = new StreamReader(mapChemin))
            {
                int Ycourant = 0;
                while (sreader.Peek() >= 0)
                {
                    string ligne = sreader.ReadLine();

                    if (Ycourant == 0)
                    {
                        string[] dim = ligne.Split(',');
                        this.Width = int.Parse(dim[0]);
                        this.Height = int.Parse(dim[0]);
                        textureCont = new int[this.Width, this.Height];

                        for (int i = 0; i < this.Width; i++)
                        {
                            for (int j = 0; j < this.Height; j++)
                            {
                                textureCont[i, j] = -1;
                            }

                        }
                    }
                    else
                    {
                        int Xcourant = 0;

                        foreach(char c in ligne.ToArray())
                        {
                            this.textureCont[Xcourant,Ycourant - 1] = int.Parse(c.ToString());
                            Xcourant++;
                        }

                    }
                    Ycourant++;
                }

            }


        }
        public void dessiner(SpriteBatch unSprite)
        {
            for (int i = 0; i < this.Width; i++)
            {
                for (int j = 0; j < this.Height; j++)
                {
                    if (this.textureCont[i,j] != -1)
                    {
                        int initY = 32 * this.textureCont[i, j];
                        Rectangle recSource = new Rectangle(0,initY,32,32);
                        Rectangle recFin = new Rectangle(i*32, j*32, 32, 32);
                        unSprite.Draw(this.texture, recFin, recSource, Color.White);
                    }
    
                }

            }

        }

    }
}
